package test.client.executor;

import test.client.thread.login.ClientLoginThread;
import test.configuration.ConfigFactory;
import test.configuration.Configuration;
import test.domain.environment.MetaUser;

public class LoginClient extends Client {
	private final int maxLoginThread = 5;
	private Configuration config = ConfigFactory.getConfiguration();
	
	public void execute() {
		int startId = config.getMetaThread().getThreadStartId();
		
		// Initialize
		this.initialize(this.maxLoginThread);
		
		// Prepare client threads
		for (int i = startId; i < startId + maxLoginThread; i++) {
			// Prepare client thread
			ClientLoginThread client = new ClientLoginThread();
			this.prepare(client, i);
			client.initialize(config.getMetaUser());
			client.setMaxPresenceCount(this.getMaxPresenceCount(i));
//			client.setMaxPresenceCount(7);
		}
		
		// Run client threads
		this.start();
		
		// Start watcher thread
		this.startWatcher();
		
		log.info("Test is completed.");
	}
	
	private int getMaxPresenceCount(int id) {
		MetaUser meta = config.getMetaUser();
		
		int modular = maxLoginThread % meta.getMaxUserCount();
		int criteria = maxLoginThread - modular;
		
		if (criteria == 0) {
			return maxLoginThread - 1;
		}
		else if (id <= criteria) {
			return meta.getMaxRosterCount();
		}
		else {
			return modular - 1;
		}
	}
	
	public static void main(String[] args) {
		Client client = new LoginClient();
		client.execute();
	}
}
